//
//  MSBeatufulDIYView.m
//  MS
//
//  Created by Yuan on 13-4-1.
//  Copyright (c) 2013年 He Yuan. All rights reserved.
//

#import "MSBeatufulDIYView.h"
#import "UIImageView+WebCache.h"

@implementation MSBeatufulDIYView

- (id)initWithFrame:(CGRect)frame
               info:(NSDictionary *)info
           delegate:(id)delegate
                tag:(int)tag
{
    self = [super initWithFrame:frame];
    if (self)
    {
        UIImage *bgImage = [UIImage imageNamed:@"diy_item_bg"];
        frame.size = bgImage.size;
        self.frame = frame;
        
        UIImageView *bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [bgImageView setImage:bgImage];
        [self addSubview:bgImageView];
        
        UIImageView *contentImageView = [[UIImageView alloc]initWithFrame:CGRectMake(12,12,bgImage.size.width - 26, bgImage.size.height - 24)];
        if ([info objectForKey:@"thumb"]) {
            [contentImageView setImageWithURL:[NSURL URLWithString:[info objectForKey:@"thumb"]]];
        }
        contentImageView.tag = tag;
        contentImageView.userInteractionEnabled = YES;
        contentImageView.clipsToBounds = YES;
        contentImageView.contentMode = UIViewContentModeScaleAspectFill;
        contentImageView.backgroundColor = [UIColor clearColor];
        [self addSubview:contentImageView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:delegate action:@selector(viewDetail:)];
        [contentImageView addGestureRecognizer:tap];
        
        txtView = [[UITextView alloc]initWithFrame:CGRectMake(contentImageView.frame.origin.x,0,contentImageView.frame.size.width,0)];
        [txtView setTextColor:[UIColor whiteColor]];
        [txtView setFont:FONT_NORMAL];
        txtView.tag = 1;
        txtView.userInteractionEnabled = NO;
        if ([[info objectForKey:@"content"] length]<1) {
            txtView.hidden = YES;
        }
        [txtView setText:[info objectForKey:@"content"]];
        [txtView setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.5]];
        [self addSubview:txtView];
        
        UIImage *barImage = [UIImage imageNamed:@"diy_item_barbg"];
        barImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, frame.size.height - barImage.size.height - 15, barImage.size.width, barImage.size.height)];
        [barImageView setImage:barImage];
        barImageView.tag = 2;
        barImageView.userInteractionEnabled = YES;
        [self addSubview:barImageView];
        
        UIViewSetFrameY(txtView, barImageView.frame.origin.y - txtView.frame.size.height);
        
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,-2, barImage.size.width- 58, barImage.size.height)];
        [titleLabel setText:[info objectForKey:@"title"]];
        [titleLabel setFont:FONT_NORMAL_BOLD];
        [titleLabel setTextColor:[UIColor whiteColor]];
        [titleLabel setBackgroundColor:[UIColor clearColor]];
        [barImageView addSubview:titleLabel];
        
        UIImage *arrow = [UIImage imageNamed:@"diy_item_arrow"];
        UIButton *arrowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [arrowBtn setImage:arrow forState:UIControlStateNormal];
        [arrowBtn addTarget:self action:@selector(showContent:) forControlEvents:UIControlEventTouchUpInside];
        [arrowBtn setFrame:CGRectMake(barImage.size.width - arrow.size.width - 22.0,barImage.size.height/2 - arrow.size.height/2 - 2.0, arrow.size.width, arrow.size.height)];
        [barImageView addSubview:arrowBtn];
        
    }
    return self;
}

-(void)showContent:(UIButton *)sender
{
    CGSize size = txtView.contentSize;
    [UIView animateWithDuration:0.3 animations:^(void){
        if (sender.tag== 0) {
            sender.transform = CGAffineTransformMakeRotation(radians(90));
            CGRect rect  = txtView.frame ;
            rect.size.height = size.height;
            rect.origin.y = barImageView.frame.origin.y - size.height;
            txtView.frame = rect;            
        }
        else
        {
            sender.transform = CGAffineTransformIdentity;
            CGRect rect  = txtView.frame ;
            rect.size.height = 0;
            rect.origin.y = barImageView.frame.origin.y;
            txtView.frame = rect;
        }
    } completion:^(BOOL f){
        sender.tag = !sender.tag;
    }];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
